package com.wf.course.service.impl;
import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.utils.page.PageBean;
import com.wf.course.dao.ICoinLogMapper;
import com.wf.course.model.goods.GoodsOrderRelation;
import com.wf.course.model.member.CoinLog;
import com.wf.course.service.ICoinLogService;

@Service
@Transactional(rollbackFor=Exception.class)
public class ICoinLogServiceImpl implements ICoinLogService{
	
	@Autowired
	private ICoinLogMapper mapper;

	@Override
	public void indexInfo(PageBean<CoinLog> pageBean) {
		// TODO Auto-generated method stub
		
		pageBean.setResults(this.mapper.queryLists(pageBean));
		pageBean.setTotalCount(this.mapper.getTotalCounts(pageBean).intValue());
		List<CoinLog> grls =  pageBean.getResults();
		if(pageBean.getResults()!=null && pageBean.getResults().size()>0){
		for(int i=0;i<pageBean.getResults().size();i++){
			CoinLog cg = pageBean.getResults().get(i);
			//根据userId,统计铜钱余额
			CoinLog zhuan = this.mapper.queryZhByUserId(cg.getUserId(), 1);
			BigDecimal zh = new BigDecimal(0) ;
			if(zhuan!=null){
				 zh = new BigDecimal(zhuan.getCoinValue());
			}
		
			CoinLog hua = this.mapper.queryZhByUserId(cg.getUserId(), 2);
			BigDecimal hu = new BigDecimal(0) ;
			if(hua!=null){
				hu = new BigDecimal(hua.getCoinValue());
			}
			cg.setCoinBalance(zh.subtract(hu));
			
			}
		}
		
	}

	@Override
	public void saveOrUpdateCoinLog(CoinLog coinLog) {
		// TODO Auto-generated method stub
		if(coinLog.getId()==null){
			this.mapper.saveCoinLog(coinLog);
		}else{
			String newValue = coinLog.getCoinValue();
			CoinLog oldCn = mapper.queryCoinLogById(coinLog.getId());
			String oldVlue = oldCn.getCoinValue();
			StringBuffer crk = new StringBuffer();
			if(oldCn.getRemarkInfo()!=null && !"".equals(oldCn.getRemarkInfo())){
				crk.append(oldCn.getRemarkInfo());
			}
			crk.append("【"+coinLog.getRemarkInfo()+",原铜钱值为："+oldVlue+"修改后为："+newValue+"】");
			
			coinLog.setRemarkInfo(crk.toString());
			this.mapper.updateCoinLog(coinLog);
		}
		
	}

	@Override
	public CoinLog queryCoinLogById(Integer id) {
		// TODO Auto-generated method stub
		return this.mapper.queryCoinLogById(id);
	}

	@Override
	public void deleteById(Integer id) {
		// TODO Auto-generated method stub
		this.mapper.deleteById(id);
	}

	@Override
	public List<CoinLog> queryCoinLogs(){
		return this.mapper.queryCoinLogs();
	}

	@Override
	public CoinLog queryCoinLogByOrderCode(String orderCode,Integer flowFlag) {
		// TODO Auto-generated method stub
		return this.mapper.queryCoinLogByOrderCode(orderCode,flowFlag);
	}
	
	public void updateCoinRemakById(String remarkInfo,Integer deleteFlag,Integer id){
		
		 this.mapper.updateCoinRemakById(remarkInfo,deleteFlag,id);
	}
	
	/**
	 * 订单退货，减去相应铜钱，更新铜钱备注
	 */
	public void updateCoinValueById(String coinValue ,String remarkInfo,Integer id){
		 this.mapper.updateCoinValueById(coinValue,remarkInfo,id);
	}

}
